const jwt = require('jsonwebtoken');
const { jwtSecret } = require('../config/service');

// 生成一个token
const generateToken = (userId) => {
  const token = jwt.sign({ userId }, jwtSecret, { expiresIn: '1h' }); // 设置过期时间为1小时
  return token;
}

// 验证token
const verifyToken = (token) => {
  try {
    const decoded = jwt.verify(token, jwtSecret);
    return decoded;
  } catch (error) {
    return null;
  }
}

module.exports = {
  generateToken,
  verifyToken
}